package com.bolt.report.business.modules.datasource.dto.converter;

import com.bolt.common.utils.StrUtil;
import com.bolt.report.business.modules.datasource.dto.ReportDataSourceDTO;
import com.bolt.report.business.modules.datasource.entity.ReportDataSourceEntity;
import com.bolt.report.definition.JdbcDataSourceDefinition;
import com.bolt.support.codegenerator.dto.TableInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/11/27.
 */
public class ReportDataSourceConverter {
    private ReportDataSourceConverter() {
// do nothing
    }

    /**
     * Entity对象转换为DTO对象
     *
     * @param reportDataSource DO对象
     * @return DTO对象
     */
    public static ReportDataSourceDTO toDTO(ReportDataSourceEntity reportDataSource) {
        if (Objects.isNull(reportDataSource)) {
            return null;
        }
        ReportDataSourceDTO result = new ReportDataSourceDTO();
        result.setId(reportDataSource.getId());

        result.setName(reportDataSource.getName());

        result.setCode(reportDataSource.getCode());

        result.setRemark(reportDataSource.getRemark());

        result.setDbType(reportDataSource.getDbType());

        result.setDbDriver(reportDataSource.getDbDriver());

        result.setDbUrl(reportDataSource.getDbUrl());

        result.setDbUsername(reportDataSource.getDbUsername());

        result.setDbPassword(reportDataSource.getDbPassword());

        result.setConnectTimes(reportDataSource.getConnectTimes());

        // TODO
        return result;
    }

    public static Map<String, String> toJsonData(TableInfo tableInfo) {
        Map<String, String> data = new HashMap<>();
        data.put("Name", tableInfo.getTableName());
        return data;
    }

    /**
     * DTO对象转换为Entity对象
     *
     * @param reportDataSource DO对象
     * @return VO对象
     */
    public static ReportDataSourceEntity toEntity(ReportDataSourceDTO reportDataSource) {
        if (Objects.isNull(reportDataSource)) {
            return null;
        }
        ReportDataSourceEntity result = new ReportDataSourceEntity();
        if(StrUtil.isNotBlank(reportDataSource.getId())){
            result.setId(reportDataSource.getId());
        }else {
            result.setId(null);
        }

        result.setName(reportDataSource.getName());
        result.setCode(reportDataSource.getCode());
        result.setRemark(reportDataSource.getRemark());
        result.setDbType(reportDataSource.getDbType());
        result.setDbDriver(reportDataSource.getDbDriver());
        result.setDbUrl(reportDataSource.getDbUrl());
        result.setDbUsername(reportDataSource.getDbUsername());
        result.setDbPassword(reportDataSource.getDbPassword());
        result.setConnectTimes(reportDataSource.getConnectTimes());
        // TODO
        return result;
    }

    public static JdbcDataSourceDefinition toDataSourceDefinition(ReportDataSourceDTO dto){
        JdbcDataSourceDefinition dataSourceDefinition=new JdbcDataSourceDefinition();
        dataSourceDefinition.setCode(dto.getCode());
        dataSourceDefinition.setName(dto.getName());
        dataSourceDefinition.setDbUrl(dto.getDbUrl());
        dataSourceDefinition.setDbDriver(dto.getDbDriver());
        dataSourceDefinition.setDbType(dto.getDbType());
        dataSourceDefinition.setDbUsername(dto.getDbUsername());
        dataSourceDefinition.setDbPassword(dto.getDbPassword());
        return dataSourceDefinition;

    }



    /**
     * DTO对象转换为DO对象
     *
     * @param reportDataSource DO对象
     * @return VO对象
     */
    public static ReportDataSourceEntity toEntity(ReportDataSourceEntity result, ReportDataSourceDTO reportDataSource) {
        if (Objects.isNull(reportDataSource)) {
            return null;
        }

        result.setName(reportDataSource.getName());
        result.setCode(reportDataSource.getCode());
        result.setRemark(reportDataSource.getRemark());
        result.setDbType(reportDataSource.getDbType());
        result.setDbDriver(reportDataSource.getDbDriver());
        result.setDbUrl(reportDataSource.getDbUrl());
        result.setDbUsername(reportDataSource.getDbUsername());
        result.setDbPassword(reportDataSource.getDbPassword());
        result.setConnectTimes(reportDataSource.getConnectTimes());
// TODO
        return result;
    }


    /**
     * 批量 Entity对象转换为DTO对象
     *
     * @param reportDataSourceList Entity集合对象
     * @return DTO对象
     */
    public static List<ReportDataSourceDTO> toDTO(List<ReportDataSourceEntity> reportDataSourceList) {
        if (Objects.isNull(reportDataSourceList)) {
            return null;
        }
        return reportDataSourceList.stream().map(ReportDataSourceConverter::toDTO).collect(Collectors.toList());
    }

}
